﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Asn1.Ocsp;
using Rabbit.API.Application.Command.Goods;
using Rabbit.API.Application.Handler.Goods;
using static System.Net.Mime.MediaTypeNames;
namespace Rabbit.API.Controllers
{
    /// <summary>
    /// 导出控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExportsController : ControllerBase
    {


        private readonly IRepository<GoodsModel> goodsway;
        private readonly IMapper mapper;
        private readonly ILogger<GoodsGetHandler> log;
        private readonly IMediator mediator;

        public ExportsController(IRepository<GoodsModel> goodsway, IMapper mapper, ILogger<GoodsGetHandler> log, IMediator mediator)
        {
            this.goodsway = goodsway;
            this.mapper = mapper;
            this.log = log;
            this.mediator = mediator;
        }
        /// <summary>
        /// 数据导出
        /// </summary>
        /// <param name="number">数据条数</param>
        /// <returns>elsx文件</returns>
        [HttpGet]
        public IActionResult GetExcelFile(int number)
        {
            try
            {
                IWorkbook workbook = new XSSFWorkbook(); //第一个工作簿
                ISheet sheet = workbook.CreateSheet("商品订单");  //第一个表
                IRow row = sheet.CreateRow(0); //第一行
                row.CreateCell(0).SetCellValue("产品名称");  //第一列
                row.CreateCell(1).SetCellValue("生产批次");  //第二列
                row.CreateCell(2).SetCellValue("保质期");  //第三列
                row.CreateCell(3).SetCellValue("生产许可证号");  //第四列
                row.CreateCell(4).SetCellValue("企业名称");  //第五列
                row.CreateCell(5).SetCellValue("封面");  //第六列
                row.CreateCell(6).SetCellValue("检验报告");  //第七列                                                     
                IQueryable<GoodsModel>? value = goodsway.GetAll().Where(x => x.IsDel == false);  //获取未删除数据表达式
                var result = value.Take(number).ToList(); //准确条数的导出数据
                for (global::System.Int32 i = 0; i < result.Count; i++)
                {
                    IRow datarow = sheet.CreateRow(i + 1); //第二+n行
                    datarow.CreateCell(0).SetCellValue(result[i].GoodsName.ToString());
                    datarow.CreateCell(1).SetCellValue(result[i].CreateBatch.ToString());
                    datarow.CreateCell(2).SetCellValue(result[i].ExpireTime.ToString());
                    datarow.CreateCell(3).SetCellValue(result[i].CreateCode.ToString());
                    datarow.CreateCell(4).SetCellValue(result[i].CreateCompany.ToString());
                    datarow.CreateCell(5).SetCellValue(result[i].Covers.ToString());
                    datarow.CreateCell(6).SetCellValue(result[i].Tests.ToString());
                }
                byte[] arr = null;
                using (var ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    arr = ms.ToArray();
                }
                return File(arr, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "商品导出信息.xlsx");


            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 获取商品导出信息
        /// </summary>
        /// <param name="request">限制条数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>未删除的商品数据</returns>
        [HttpGet]
        public Task<APIResult<List<GoodsModel>>> GoodsGetAllData([FromQuery] GoodsGetAllDataCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
